import 'dart:ffi';

import 'package:flutter/material.dart';
import 'package:flutter_architecture/constant/route_constants.dart';
import 'package:flutter_architecture/routes/page_builder.dart';
import 'package:flutter_architecture/views/common/navigation_drawer_page.dart';
import 'package:flutter_architecture/views/common/not_find_page.dart';
import 'package:flutter_architecture/views/common/splash_page.dart';
import 'package:flutter_architecture/views/common/welcome_page.dart';
import 'package:flutter_architecture/views/home/home_page.dart';
import 'package:flutter_architecture/views/index_page.dart';
import 'package:flutter_architecture/views/login/login_page.dart';
import 'package:flutter_architecture/views/my/my_page.dart';
import 'package:fluro/fluro.dart';

/// 基于fluro路由管理
/// 原生路由跳转
/// @author: dingwen
/// @date: 2020/12/28

class Routes {
  static final fluroRouter = FluroRouter();

  static final Map<String, PageBuilder> pageRoutes = {
    root: PageBuilder(builder: (bundle) => IndexPage()),
    splash_page: PageBuilder(builder: (bundle) => SplashPage()),
    welcome_page: PageBuilder(builder: (bundle) => WelcomePage()),
    index_page: PageBuilder(builder: (bundle) => IndexPage()),
    home_page: PageBuilder(builder: (bundle) => HomePage()),
    my_page: PageBuilder(builder: (bundle) => MyPage()),
    navigation_drawer__page: PageBuilder(builder: (bundle) => NavigationDrawerPage()),
  };

  static setUpRoutes() {
    pageRoutes.forEach((path, handler) {
      fluroRouter.define(path, handler: handler.getHandler());
    });
    // 404页面设置
    fluroRouter.notFoundHandler = Handler(handlerFunc: (context, params) {
      return NotFindPage();
    });
  }

  ///页面跳转
  static Future<dynamic> pageTo(BuildContext context, String path,
      {TransitionType transitionType = TransitionType.fadeIn}) {
    return fluroRouter.navigateTo(
      context,
      path,
      transition: transitionType,
      transitionDuration: Duration(seconds: 3)//todo 细节等待确定
    );
  }

  ///带权限的页面跳转
  static Future<dynamic> authTo(BuildContext context, String path,{TransitionType transitionType = TransitionType.fadeIn}) {
    // TODO: 验证权限，再决定向哪跳转
    var auth = true;
    if (auth) {
      return fluroRouter.navigateTo(
        context,
        path,
        transition: transitionType,
      );
    } else {
      // 验证失败，跳转到了login
      return fluroRouter.navigateTo(
        context,
        login_page,
        transition: transitionType,
      );
    }
  }

  ///跳转到指定页面
  static push(BuildContext context, {RouteSettings settings}) {
    Navigator.push(context, findRoutes(settings: settings));
  }

  ///根据路由名称找到页面
  static Route findRoutes({RouteSettings settings}) {
    String name = settings?.name;
    Object arguments = settings?.arguments;
    return MaterialPageRoute(builder: (context) {
      return _findPage(name, arguments);
    });
  }

  ///返回匹配到的页面
  static Widget _findPage(String name, Object arguments) {
    Widget page;
    switch (name) {
      case splash_page:
        page = SplashPage();
        break;
      case index_page:
        page = IndexPage();
        break;
      case welcome_page:
        page = WelcomePage();
        break;
      case my_page:
        page = MyPage();
        break;
      case login_page:
        page = LoginPage();
        break;
      case home_page:
        page = HomePage();
        break;
      default:
        return NotFindPage();
    }
    return page;
  }
}
